package asyncTasks;

import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.net.ParseException;
import android.os.AsyncTask;
import basicClasses.Friend;
import basicClasses.Friends;
import basicClasses.LoginObject;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class GetRequests extends
		AsyncTask<LoginObject, Void, ArrayList<String>> {
	ArrayList<String> requests = new ArrayList<String>();

	@Override
	protected ArrayList<String> doInBackground(LoginObject... params) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://softeng.cs.uwosh.edu/students/cs342g4/get_requests.php");
		String resp = "";
		try
		{
			// Convert the login object to XML
			XStream xstream = new XStream(new DomDriver("UTF-8"));
			xstream.alias("Login", LoginObject.class);
			String xml = xstream.toXML(params[0]);
			StringEntity se = new StringEntity(xml, HTTP.UTF_8);
			se.setContentType("text/xml");
			httppost.setEntity(se);
			HttpResponse httpresponse = httpclient.execute(httppost);
			HttpEntity resEntity = httpresponse.getEntity();
			resp = EntityUtils.toString(resEntity);
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	 
		
		System.out.println(resp);
		System.out.println("sldkjsldfjksljfdlsjdflsjdflsjfdl");
		if(resp.equalsIgnoreCase("FALSE"))
		{
			return requests;
		}
		else{
		XStream xStream = new XStream();
		xStream.processAnnotations(new Class[] { Friends.class, Friend.class });
		Friends comments = (Friends)xStream.fromXML(resp);
		ArrayList<Friend> arr = comments.getArray();
		for(Friend x : arr)
		{
			requests.add(x.friendName);
		}
		return requests;
		}
	}

}
